package com.littleu.yojoincn.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.littleu.yojoincn.common.enumeration.ReserveStatusEnum;
import com.littleu.yojoincn.common.exception.http.NotFoundException;
import com.littleu.yojoincn.dto.reserve.ReserveCreateDeliveryDTO;
import com.littleu.yojoincn.dto.reserve.ReserveDeliveryItemDTO;
import com.littleu.yojoincn.dto.reserve.ReserveQueryDTO;
import com.littleu.yojoincn.logic.reserve.DeliveryChecker;
import com.littleu.yojoincn.mapper.ReserveDeliveryMapper;
import com.littleu.yojoincn.mapper.ReserveMapper;
import com.littleu.yojoincn.model.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReserveService extends ServiceImpl<ReserveMapper, ReserveDO> {
    @Autowired
    private ReserveDeliveryService reserveDeliveryService;
    @Autowired
    private ReserveDeliveryItemService reserveDeliveryItemService;

    public IPage<ReserveDO> getReserveByPage(Integer page, Integer count, ReserveQueryDTO dto){
        Page<ReserveDO> pager = new Page<>(page, count);
        QueryWrapper<ReserveDO> queryWrapper = new QueryWrapper<>();
        if(dto.getStatus() == 0){
            queryWrapper.lambda().eq(ReserveDO::getCelebrityId, dto.getCelebrityId()).orderByDesc(ReserveDO::getPayTime);
            return this.getBaseMapper().selectPage(pager, queryWrapper);
        }

        queryWrapper.lambda().eq(ReserveDO::getStatus, dto.getStatus())
                .eq(ReserveDO::getCelebrityId, dto.getCelebrityId())
                .orderByDesc(ReserveDO::getPayTime);
        return this.getBaseMapper().selectPage(pager, queryWrapper);
    }

    public ReserveDetailDO getDetail(Long id) {
        return this.getBaseMapper().selectReserveDetailByReserveId(id);
    }

    public List<ReserveDeliveryDetailDO> getReserveDeliveryWithDetail(Long id){
        return this.reserveDeliveryService.getBaseMapper().selectReserveDeliveryDetailByReserveId(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void createDelivery(DeliveryChecker deliveryChecker, ReserveCreateDeliveryDTO dto, Long id) {
        ReserveDO reserveDO = deliveryChecker.getReserveDO();
        ReserveDeliveryDO reserveDeliveryDO = new ReserveDeliveryDO();
        BeanUtils.copyProperties(dto, reserveDeliveryDO);
        reserveDeliveryDO.setReserveId(id);
        reserveDeliveryDO.setCelebrityId(deliveryChecker.getReserveDO().getCelebrityId());
        reserveDeliveryDO.setShopCode(reserveDO.getShopCode());
        reserveDeliveryDO.setShopName(reserveDO.getShopName());
        reserveDeliveryDO.setTotalCount(deliveryChecker.getTotalCount());
        reserveDeliveryDO.setTotalPrice(deliveryChecker.getServerTotalPrice());
        this.reserveDeliveryService.save(reserveDeliveryDO);
        if(dto.getSkuList() != null){
            this.insertDeliveryItem(dto.getSkuList(), reserveDeliveryDO);
        }
        //修改reserve为发货中
        reserveDO.setStatus(ReserveStatusEnum.SHIPPED.getValue());
        reserveDO.setVirtualBalance(reserveDO.getVirtualBalance().subtract(deliveryChecker.getServerTotalPrice()));
        this.updateById(reserveDO);
    }

    private void insertDeliveryItem(List<ReserveDeliveryItemDTO> deliveryItemDTOList, ReserveDeliveryDO deliveryDO){
        List<ReserveDeliveryItemDO> itemDOList = deliveryItemDTOList
                .stream()
                .map(deliveryItemDTO->{
                    ReserveDeliveryItemDO reserveDeliveryItemDO = new ReserveDeliveryItemDO();
                    BeanUtils.copyProperties(deliveryItemDTO, reserveDeliveryItemDO);
                    reserveDeliveryItemDO.setDeliveryId(deliveryDO.getId());
                    reserveDeliveryItemDO.setPlatformCode(deliveryDO.getPlatformCode());
                    reserveDeliveryItemDO.setCelebrityId(deliveryDO.getCelebrityId());
                    reserveDeliveryItemDO.setShopCode(deliveryDO.getShopCode());
                    reserveDeliveryItemDO.setShopName(deliveryDO.getShopName());
                    return reserveDeliveryItemDO;
                }).collect(Collectors.toList());
        this.reserveDeliveryItemService.saveBatch(itemDOList);
    }

    public DeliveryChecker isOK(ReserveCreateDeliveryDTO dto, Long id){
        ReserveDO reserveDO = this.getById(id);
        DeliveryChecker deliveryChecker = new DeliveryChecker(dto, reserveDO);
        deliveryChecker.isOK();
        return deliveryChecker;
    }
}
